package com.looncin.oj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.looncin.oj.common.ErrorCode;
import com.looncin.oj.constant.UserConstant;
import com.looncin.oj.exception.BusinessException;
import com.looncin.oj.exception.ThrowUtils;
import com.looncin.oj.mapper.FilesMapper;
import com.looncin.oj.mapper.UserAccessFilesMapper;
import com.looncin.oj.mapper.UserMapper;
import com.looncin.oj.model.entity.*;
import com.looncin.oj.service.FilesService;
import com.looncin.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
@Slf4j
public class FilesServiceImpI extends ServiceImpl<FilesMapper, Files> implements FilesService {
    @Resource
    private UserService userService;
    @Resource
    private FilesMapper filesMapper;
    @Resource
    private UserAccessFilesMapper userAccessFilesMapper;
    @Override
    public long FilesUpload(String files_name,String files_type,String content,String other,long parent_folder_id, User loginUser) {
        // 创建时，参数不能为空
        if (true) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(files_name, files_type,content,other), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(files_name) && files_name.length() > 42) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件名称过长");
        }
        if (StringUtils.isNotBlank(files_type) && files_type.length() > 42) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件类型过长");
        }
        if (StringUtils.isNotBlank(content) && content.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容过长");
        }
        if (StringUtils.isNotBlank(other) && other.length() > 240) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "附加内容过长");
        }
        if(  loginUser.getUserRole()== UserConstant.ADMIN_ROLE || loginUser.getUserRole()==UserConstant.TEACHER_ROLE   ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文件夹作者权限不足");
        }
        Files file = new Files();
        file.setFiles_name(files_name);
        file.setFiles_type(files_type);
        file.setContent(content);
        file.setOther(other);
        file.setParent_folder_id(parent_folder_id);
        file.setCreator_id(loginUser.getId());
        synchronized (file) {
            // 3. 插入数据

            boolean saveResult = this.save(file);
            if (!saveResult) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "FilesUpload文件上传失败,数据库出现问题");
            }
            return file.getId();
        }
    }

    @Override
    public boolean IsUserPowerFiles(Long file_id, Long user_id) {

        return false;
    }



    @Override
    public List<Files> FilesSelectByCreatorId(User loginUser) {
        //判断用户是否为管理员是则返回该管理员创建全部的文件夹
        if( loginUser.getUserRole() == UserConstant.ADMIN_ROLE ){
            return filesMapper.findFilesByCreatorIdWithIndex(loginUser.getId());
        }
        return null;
    }

    @Override
    public List<Files> UserByUserIdSelectFiles(User loginUser) {
        //判断用户是否登录
        if( Objects.isNull(loginUser) ) {
            return null;
        }
        if( loginUser.getUserRole() == UserConstant.USER_LOGIN_STATE ){
            //查找用户被授权可以访问的文件
//            List<UserAccessFile> ListUserAccessFile = userAccessFilesMapper.findFilesByVisitorUserIdWithIndex(loginUser.getId());
//            List<Long> ListFileId = ListUserAccessFile.stream()
//                    .map( UserAccessFile::getFile_id ) // 使用方法引用获取 MyObject 对象的 value 属性值
//                    .collect(Collectors.toList());
//            System.out.println(ListFileId);
//            List<Files> ListFiles = filesMapper.selectBatchIds(ListFileId);
            //通过mysql的存储结构oj.get_files_child_list获取到该用户id中被授权访问的文件


            return filesMapper.getUserAccessibleFiles(loginUser.getId());
        }

        return null;
    }


}
